# -*- coding: utf-8 -*-
import os
import sys

import faker

from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QDialog
from PyQt5.uic import loadUi
from PyQt5 import QtCore

from drop_box_list import DROP_BOX_LIST
from gen_data import gen_data_from_template


class TDG(QDialog):
    """TDG窗口"""
    CN_faker = faker.Faker('zh-CN')
    EN_faker = faker.Faker()

    def __init__(self, *args):
        super(TDG, self).__init__(*args)
        self.faker = self.CN_faker

        # 兼容打包前后
        bundle_dir = sys._MEIPASS if getattr(sys, 'frozen', False) else os.path.dirname(os.path.abspath(__file__))
        # 加载ui文件
        loadUi(os.path.join(bundle_dir, 'tdg.ui'), self)
        self.clipboard = QApplication.clipboard()  # 剪切板实例
        self.init_sign()  # 初始化自定义槽方法
        self.more_index = 0  # 下拉框对应index
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
        self.setFixedSize(284, 256)
        self.show()

    def change_faker(self):
        """
        切换中英文
        """
        self.faker = self.CN_faker if self.isCN.isChecked() else self.EN_faker

    def slot_data(self, tag):
        """
        调用faker的api生成数据并放入剪切板
        :param tag: 传入的faker的api名字
        :return:
        """
        res = {"faker": self.faker}
        exec(f"data=faker.{tag}()", res)
        self.clipboard.setText(str(res['data']))

    def more_data(self, i):
        """
        获取下拉框index
        :param i: 传入的index
        :return:
        """
        self.more_index = i
        res = DROP_BOX_LIST[self.more_index][1]
        return res

    def gen_data(self):
        """生成数据"""
        tmp = self.templates_text.toPlainText()
        times = self.times.toPlainText()
        try:
            times = int(times)
        except ValueError:
            times = 10
        if tmp and times:
            gen_data_from_template(tmp, times)

    def init_sign(self):
        """
        初始化信号与槽
        :return:
        """
        # 常用按钮
        self.name.clicked.connect(lambda: self.slot_data('name'))
        self.address.clicked.connect(lambda: self.slot_data('address'))
        self.phone.clicked.connect(lambda: self.slot_data('phone_number'))
        self.email.clicked.connect(lambda: self.slot_data('email'))
        self.ip.clicked.connect(lambda: self.slot_data('ipv4'))
        self.uuid.clicked.connect(lambda: self.slot_data('uuid4'))
        self.date.clicked.connect(lambda: self.slot_data('date'))
        self.datetime.clicked.connect(lambda: self.slot_data('past_datetime'))
        self.people_id.clicked.connect(lambda: self.slot_data('ssn'))
        # 中英文切换
        self.isCN.clicked.connect(self.change_faker)
        # 下拉框
        self.more.addItems([item[0] for item in DROP_BOX_LIST])
        self.more.activated[int].connect(self.more_data)
        self.gen.clicked.connect(lambda: self.slot_data(self.more_data(self.more_index)))

        # 文本框

        self.gen_2.clicked.connect(self.gen_data)


app = QApplication(sys.argv)
w = TDG()
sys.exit(app.exec_())
